proc bot:message {xlib from type x args} {
    set id ""
    set body ""
    set is_subject 0
    set subject ""
    set thread ""
    set priority ""
    set err {}
    foreach {key val} $args {
	switch -- $key {
	    -id {
		set id $val
	    }
	    -body {
		set body $val
	    }
	    -subject {
		set is_subject 1
		set subject $val
	    }
	    -error {
		set err $val
	    }
	}
    }

    debugmsg chat "MESSAGE: $xlib; $from; $id; $type; $is_subject;\
		   $subject; $body; $err; $thread; $priority; $x"
	if {$x!={}} {
		foreach xa $x {
			::xmpp::xml::split $xa tag xmlns attrs cdata subels

			switch -- $xmlns \
			$::NS(xconference) {
				set xconference_group [::xmpp::xml::getAttr $attrs jid]
				set xconference_password ""
					if {[string equal $body ""] && ![string equal $cdata ""]} {
						set xconference_body $cdata
					} else {
						set xconference_body $body
					}
					hook::run incoming_invite $xlib $inviter $from "$xconference_body \n $reason"
			} \
			$::NS(muc#user) {
				set password ""
				set inviter ""
				foreach ch $subels {
					::xmpp::xml::split $ch stag sxmlns sattrs scdata ssubels

				    switch -- $stag {
						invite {
							set inviter [::xmpp::xml::getAttr $sattrs from]
							if {![string equal $inviter ""]} {
								foreach sch $ssubels {
								    ::xmpp::xml::split $sch sstag ssxmlns ssattrs \
											    sscdata sssubels
								    if {[string equal $sstag "reason"]} {
										set reason $sscdata]
									}
								}
							}
						}
						password {
								set password $scdata
						}
					}
				}
				if {![string equal $inviter ""]} {
					set muc_group $from
					set muc_password $password
				}
			}
		}
	}
	hook::run message $xlib $from $id $type $is_subject $subject $body $err $thread $priority $x
}
proc bot:log {args} {
debugmsg bot "==========================\n$args\n============================"
}
proc bot:disconnect {args} {
debugmsg bot "==========================\n$args\n============================"
}
proc bot:iq {args} {
debugmsg bot "==========================\n$args\n============================"
}
proc bot:error {args} {
debugmsg bot "ERROR\n $args"
}

proc ::bgerror {err} {
    global errorInfo

    ::LOG "$err\n$errorInfo"
}
proc ::LOG {text} {
    puts stderr $text
}